/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.elcom.emocbc.cbcgateway.smscbresponse;

import com.elcom.emocbc.mongodb.CellBroadcastResult;
import com.elcom.util.Constant;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import org.apache.log4j.PropertyConfigurator;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

/**
 *
 * @author GiangLT
 */
public class GetMessageStatusResponseHandler extends DefaultHandler {

    private List<CellBroadcastResult> cellBroadcastResults = new ArrayList<CellBroadcastResult>();
    private CellBroadcastResult cellBroadcastResult;
    private String temp;
    private String messageId;
    private String bscId;

    @Override
    public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
        if (qName.equals("broadcastInformation")) {
            cellBroadcastResult = new CellBroadcastResult();
        }

    }

    @Override
    public void endElement(String uri, String localName, String qName) throws SAXException {
        if (qName.equals("messageId")) {
            messageId = temp;
        }
        if (qName.equals("bscId")) {
            bscId = temp;
        }
        if (qName.equals("cellId")) {
            cellBroadcastResult.setCellKey(temp);
        }
        if (qName.equals("numberOfBroadcastComplete")) {
            cellBroadcastResult.setBroadcastFailed(Boolean.FALSE);
            cellBroadcastResult.setNumberOfBrodcastComplete(Integer.valueOf(temp));
        }
        if (qName.equals("cause")) {
            cellBroadcastResult.setBroadcastFailed(Boolean.TRUE);
            cellBroadcastResult.setFailureCause(temp);
        }
        if (qName.equals("broadcastInformation")) {
            cellBroadcastResult.setBscId(bscId);
            cellBroadcastResult.setBroadcastMessageId(messageId);
            cellBroadcastResults.add(cellBroadcastResult);
        }
    }

    @Override
    public void characters(char[] ch, int start, int length) throws SAXException {
        temp = new String(ch, start, length);
    }

    public List<CellBroadcastResult> getCellBroadcastResults() {
        return cellBroadcastResults;
    }
    
    public static void main(String [] args) {
        try {
            PropertyConfigurator.configureAndWatch(Constant.CONFIG_FOLDER + Constant.MODULE_NAME + "-log4j.props");
            FileInputStream fileInputStream  = new FileInputStream("F:\\Projects\\DailyInfo\\docs\\getmessagestatus.xml");
            SAXParserFactory factory = SAXParserFactory.newInstance();
            SAXParser saxParser = factory.newSAXParser();
            GetMessageStatusResponseHandler defaultHandler = new GetMessageStatusResponseHandler();
            saxParser.parse(fileInputStream, defaultHandler);
            
        } catch (ParserConfigurationException ex) {
            java.util.logging.Logger.getLogger(GetMessageStatusResponseHandler.class.getName()).log(Level.SEVERE, null, ex);
        } catch (SAXException ex) {
            java.util.logging.Logger.getLogger(GetMessageStatusResponseHandler.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            java.util.logging.Logger.getLogger(GetMessageStatusResponseHandler.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}
